﻿using System.Windows;
using System.Windows.Input;
using ControleRepresentanteSync.Application;

namespace ControleRepresentanteSync.View.Wizard
{
    public partial class WizardWindow : IWindow
    {
        public WizardWindow()
        {
            InitializeComponent();
            this.SetIcon(Properties.Resources.Idle);
        }

        private void GoBackClick(object sender, RoutedEventArgs e)
        {
            ((WizardViewModel) DataContext).FireGoBack();
        }

        private void GoForwardClick(object sender, RoutedEventArgs e)
        {
            GoForward();
        }

        private void GoForward()
        {
            WizardViewModel wizardViewModel = (WizardViewModel)DataContext;
            if (wizardViewModel.CurrentPage.IsValid)
            {
                wizardViewModel.FireGoForward();
            }
        }

        private void CancelClick(object sender, RoutedEventArgs e)
        {
            Cancel();
        }

        private void Cancel()
        {
            ((WizardViewModel)DataContext).FireCancel();
        }

        private void WindowDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            WizardViewModel oldDataContext = (WizardViewModel) e.OldValue;
            if (oldDataContext != null)
            {
                oldDataContext.Close -= WizardViewModelClose;
            }

            WizardViewModel newDataContext = (WizardViewModel) e.NewValue;
            if (newDataContext != null)
            {
                newDataContext.Close += WizardViewModelClose;
            }
        }

        private void WizardViewModelClose(object sender, WizardCloseEventArgs e)
        {
            DialogResult = e.Result;
            Close();
        }

        private void WindowKeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Enter:
                    GoForward();
                    break;
                case Key.Escape:
                    Cancel();
                    break;
            }
        }
    }
}
